const query = {
    pagenum: 1, // 是	int	页码值
    pagesize: 2, // 是	int	每页显示多少条数据
    cate_id: "", // "" 所有的文章分类 否	string	文章分类的 Id
    state: "", // "" 所有状态 文章状态  可选值有：已发布、草稿
};
let form = layui.form
getlist()
function getlist() {
    axios({
        method: 'get',
        url: '/my/article/list',
        params: query
    }).then(({ data: { data, total } }) => {
        // console.log(data);
        let newArr = data.map(({ title, cate_name, pub_date, state, Id }) => {
            return `<tr>
                        <td>${title}</td>
                        <td>${cate_name}</td>
                        <td>${pub_date}</td>
                        <td>${state}</td>
                        <td>
                        <button type="button" class="layui-btn layui-btn-xs btn_edit" data-id="${Id}">编辑</button>
                        <button type="button" class="layui-btn layui-btn-danger layui-btn-xs btn_delete" data-id="${Id}">删除</button>
                        </td>
                    </tr>`
        }).join('')
        $('#list').html(newArr)
        renderPage(total)
    })
}

getrander()
function getrander() {
    axios({
        method: 'get',
        url: '/my/article/cates',
    }).then(({ data: { data } }) => {
        const str = data.map(item => {
            return ` <option value="${item.Id}">${item.name}</option>`
        }).join('')
        $('#cateSelect').append(str)
        // console.log(str);
        form.render()
    })
}

$('#form').on('submit', function (e) {
    e.preventDefault()
    query.cate_id = $('#cateSelect').val()
    query.state = $('#stateSelect').val()
    query.pagenum = 1
    getlist()
})


let laypage = layui.laypage;
function renderPage(count) {

    //执行一个laypage实例
    laypage.render({
        elem: 'page-box' //注意，这里的 test1 是 ID，不用加 # 号
        , count, //数据总数，从服务端得到
        limit: query.pagesize,
        limits: [1, 2, 3, 4, 5, 6],
        curr: query.pagenum,
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump:function(obj,first){
            //页码数要随着变化
           query.pagenum = obj.curr
           query.pagesize = obj.limit
            // console.log(obj.curr);
            if(!first){
                getlist()
            }
        }
    });
}

$('tbody').on('click','.btn_delete',function(){
    const id = $(this).attr('data-id')
    // console.log(id);
    layer.confirm('确定删除此文章吗?',
     {icon: 3, title:'提示'}, 
     function(index){
        //do something
        
        axios.get('/my/article/delete/' +id).then(res => {
            console.log(res);
            if(res.data.status !== 0) return layer.msg(res.data.message)
            layer.msg(res.data.message)
            if($('.btn_delete').length === 1){
                query.pagenum = query.pagenum === 1 ? 1 : query.pagenum - 1
            }
            layer.close(index);
            getlist()
        })
      });
})

$('tbody').on('click','.btn_edit',function(){

    const id = $(this).attr('data-id')
    location.href = `/article/art_edit.html?id=${id}`
})